<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>history新四样</title>
    <style>
      * {
        font-size: 28px;
      }
    </style>
  </head>

  <body>
    <div>
      state:
      <div id="stateValue"></div>
      <div>历史记录长度:<span id="hlength"></span></div>
    </div>
    <div>
      <br />
      <a href="#index">#index</a><br />
      <button type="button" id="btnPushState">pushState</button><br />
      <button type="button" id="btnReplaceState">replaceState</button><br />
    </div>

    <script>
      let index = 0;
      hlength.textContent = history.length;
      btnPushState.onclick = function (ev) {
        index++;
        history.pushState({ data: "pushState" + index }, "", `/pushState${index}.html`);
        hlength.textContent = history.length;
      };

      btnReplaceState.onclick = function (ev) {
        history.replaceState({ data: "replaceState" }, "", "/replaceState.html");
        hlength.textContent = history.length;
      };

      window.onpopstate = function (ev) {
        console.log("onpopstate trigger");
        stateValue.textContent = JSON.stringify(ev.state);
        hlength.textContent = history.length;
      };
    </script>
  </body>
</html>
